<?php
/**
 * Created by PhpStorm.
 * User: zeopean
 * Date: 2015/8/12
 * Time: 12:00
 */
namespace App\Http\Controllers\Api;

use App\District;
use App\Exceptions\NotFoundException;
use App\Exceptions\ProjectException;
use App\Exceptions\ProjectMilestoneException;
use App\Fields;
use App\Http\Requests\ProjectMilestoneRequest;
use App\Http\Requests\ProjectRequest;
use App\Http\Requests\ProjectNewsRequest;
use App\Exceptions\SecurityException;

use App\Http\Requests\ProjectTeamRequest;
use App\Images;
use App\InvestLog;
use App\Phases;
use App\Project;
use App\ProjectMilestone;
use App\ProjectNews;
use App\ProjectTeam;
use App\ProjectUrl;
use App\User;
use App\UserInfo;
use App\UserTeams;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
use phpDocumentor\Reflection\DocBlock\Type\Collection;
use App\Services\TagsManager;

class ProjectController extends ApiController{
    /**
     * @param Guard $guard
     * @param ProjectRequest $request
     * @param int $id
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     * @throws ProjectException
     * @throws SecurityException
     */
    public function EditProject(Guard $guard,ProjectRequest $request ,$id=0)
    {
        /** 判断用户是否登录*/
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var User  $currentUser */
        $currentUser = $guard->user();
        if(!$currentUser)
            throw new SecurityException(SecurityException::NoPermission);

        /** 处理项目标签 */
        if(!is_null($request->get('tag'))){
            $tagsManager = new TagsManager();
            $tagsManager->EditTags($request);
        }

        $Project = Project::find($id);
        if($id > 0 && Project::whereId($id)->exists()){
            if($request->get('is_admin') != 0)
                $Project = Project::whereId($id)->whereUserId($currentUser->id)->first();
        }else if($id == 0 && !Project::whereId($id)->exists()){
            $Project = new Project();

            /**判断项目名是否存在*/
            if(ProjectRequest::ProjectNameExists($request->get('project_name')))
                throw new ProjectException(ProjectException::ProjectNameExists);

        }else if($id < 0){
            throw new ProjectException(ProjectException::CreateProjectFirst);
        }

        /**联系方式进行判断*/
        if($request->get('phone')!== null && !ProjectRequest::isPhone($request->get('phone')))
            throw new ProjectException(ProjectException::PhoneIsError);


        $Project->project_name      = !is_null($request->get('project_name')) ? $request->get('project_name') :$Project->project_name;                    //项目名称
        $Project->project_intro     = !is_null($request->get('project_intro')) ?$request->get('project_intro') : $Project->intro;                          //项目简介
        $Project->img_projectlogo_id= !is_null($request->get('img_projectlogo_id')) ? $request->get('img_projectlogo_id') : $Project->img_projectlogo_id;  //项目logo
        $Project->company_name      = !is_null($request->get('company_name')) ? $request->get('company_name') : $Project->company_name;                    //公司名称
        $Project->province          = !is_null($request->get('province')) ? $request->get('province') : $Project->province;                                //所在省份
        $Project->city              = !is_null($request->get('city')) ? $request->get('city') : $Project->city;                                            //所在城市
        $Project->create_time       = !is_null($request->get('create_time')) ? $request->get('create_time') : $Project->create_time;                       //成立时间
        $Project->phase_id          = !is_null($request->get('phase_id')) ? $request->get('phase_id') : $Project->phase_id;                                //项目发展阶段
        $Project->project_status    = !is_null($request->get('project_status')) ? $request->get('project_status'):$Project->project_status;                //项目状态 =》demo、上线、盈利
        $Project->project_desc      = !is_null($request->get('project_desc')) ? $request->get('project_desc'):$Project->project_desc;                      //项目介绍
        $Project->project_lightspot = !is_null($request->get('project_lightspot')) ? $request->get('project_lightspot') : $Project->project_lightspot;     //项目亮点
        $Project->project_future    = !is_null($request->get('project_future')) ? $request->get('project_future') : $Project->project_future;              //行业前景
        $Project->file_bp_id        = !is_null($request->get('file_bp_id')) ? $request->get('file_bp_id') : $Project->file_bp_id;                          //商业计划书
        $Project->invet_phase_id    = !is_null($request->get('invet_phase_id')) ? $request->get('invet_phase_id'):$Project->invet_phase_id;                //融资阶段
        $Project->invet             = !is_null($request->get('invet')) ? $request->get('invet') : $Project->invet;                                         //融资金额
        $Project->money_type        = !is_null($request->get('money_type')) ? $request->get('money_type') : $Project->money_type;                          //货币单位
        $Project->min_money         = !is_null($request->get('min_money')) ? $request->get('min_money') : $Project->min_money;                             //最低融资
        $Project->sell_stock        = !is_null($request->get('sell_stock')) ? $request->get('sell_stock') : intval($Project->sell_stock);                  //出让股权
        $Project->invest_num        = !is_null($request->get('invest_num')) ? $request->get('invest_num') : $Project->invest_num;                          //投资方数量
        $Project->stop_time         = !is_null($request->get('stop_time')) ? $request->get('stop_time') : $Project->stop_time;                             //截止日期
        $Project->reginv_time       = !is_null($request->get('reginv_time')) ? $request->get('reginv_time') : $Project->reginv_time;                       //申请时间
        $Project->is_finance        = !is_null(Input::get('is_finance')) ? Input::get('is_finance') : 0;                                                   //是否申请融资
        $Project->is_speed_finance  = !is_null($request->get('is_speed_finance')) ? $request->get('is_speed_finance') : 0;                                 //是否快速融资
        $Project->repay_rate        = !is_null($request->get('repay_rate')) ? $request->get('repay_rate') : $Project->repay_rate;                          //其他描述
        $Project->star_layer        = !is_null($request->get('star_layer')) ? $request->get('star_layer') : $Project->star_layer;                          //其他描述
        $Project->other_desc        = !is_null($request->get('other_desc')) ? $request->get('other_desc') : $Project->other_desc;                          //其他描述
        $Project->name              = !is_null($request->get('name')) ? $request->get('name') : $Project->name;                                            //联系人姓名
        $Project->email             = !is_null($request->get('email')) ? $request->get('email') : $Project->email;
        $Project->phone             = !is_null($request->get('phone')) ? $request->get('phone') : $guard->user()->phone;                                   //联系手机
        if($Project->project_auth==null) $Project->project_auth = 0;
        $Project->project_auth      = !is_null($request->get('project_auth')) ? $request->get('project_auth') : $Project->project_auth;                                         //项目认证状态
        $webUrl = Input::get('web_url');
        $iosUrl = Input::get('ios_url');
        $androidUrl = Input::get('android_url');
        if($request->get('is_admin')!==null || $request->get('is_admin')!==0)$Project->user_id = $guard->user()->id;          //所属用户id (排除管理员)

        if(!$Project->repay_rate) $Project->repay_rate = 0 ;
        if(!$Project->star_layer) $Project->star_layer = 0 ;
        $Project->save();



        $fields = $request->get('fields');
        if(!is_null($fields)){
            $fields = array_diff($fields ,$Project->project_x_fields()->lists('field_id'));
            if($fields)
                $Project->project_x_fields()->attach($fields);
        }

        $userTeam = $request->get('teamname');
        if(!is_null($userTeam)){
            $Project->project_x_team()->attach($userTeam);
        }

        /** 项目总览图片模块 */
        if(!is_null($request->get('demo_img_id'))){
            /** 对id进行处理 */
            $idsStr = $request->get('demo_img_id');
            $idsStr = trim($idsStr , ',');
            $ids = explode(',' , $idsStr);
            $Project->project_x_overview_img()->attach($ids);
        }

        if(!empty($webUrl)) {
            ProjectUrl::firstOrCreate(['url_type'=> 'web','url_name'=>$webUrl,'project_id'=>$Project->id]);
        }

        if(!empty($iosUrl)) {
            ProjectUrl::firstOrCreate(['url_type'=> 'ios','url_name'=>$iosUrl,'project_id'=>$Project->id]);
        }

        if(!empty($androidUrl)) {
            ProjectUrl::firstOrCreate(['url_type'=> 'android','url_name'=>$androidUrl,'project_id'=>$Project->id]);
        }
        Session::forget('ImageId');
        Session::put('projectId',$Project->id);

        return $this->buildResponse(trans('api.project_edit.success') , $Project);
    }

    /**
     * 通过项目标识获取图片(创业经历项目)
     * @param $projectId
     * @return mixed
     * @throws NotFoundException
     */
    public function getProjectLogoByProjectId($projectId)
    {
        if(!$projectId)
            return \Response::download('images/default_photo.jpg','default_photo.jpg',[],'inline');

        $project = Project::whereId($projectId)->first();
        if(!$project)
            throw new NotFoundException(NotFoundException::PhotoNotFound);

        $photo = $project->toProjectLogo;
        if(!$photo || $photo == null)
            return \Response::download('images/default_photo.jpg','default_photo.jpg',[],'inline');

        $photoFile = $photo->files;
        return \Response::download($photoFile->getLocalPath(),$photoFile->file_name,[],'inline');
    }

    /**
     * 编辑项目url
     *
     * @param Guard $guard
     * @param array $data
     * @param int $id
     * @return mixed
     *
     * @throws SecurityException
     */
    public function EditProjectUrl(Guard $guard , $data=array() ,$id=0)
    {
        /** 判断用户是否登录*/
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var User  $currentUser */
        $currentUser = $guard->user();
        if(!$currentUser)
            throw new SecurityException(SecurityException::NoPermission);

        if($id != 0 && ProjectUrl::whereId($id)->exists()){
            $projectUrl = ProjectUrl::find($id);
        }else{
            $projectUrl = new ProjectUrl();
        }

        if(!is_null($data['project_id'])) $projectUrl->project_id   = $data['project_id'];
        if(!is_null($data['url_type'])) $projectUrl->url_type       = $data['url_type'];
        if(!is_null($data['url_name'])) $projectUrl->url_name       = $data['url_name'];

        $projectUrl->save();
        return $projectUrl::whereUrlName($data['url_name'])->pluck('id');
    }

    /**
     * @param Guard $guard
     * @param ProjectMilestoneRequest $request
     * @param int $id
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     * @throws ProjectMilestoneException
     * @throws SecurityException
     */
    public function EditProjectMilestone(Guard $guard , ProjectMilestoneRequest $request , $id=0)
    {
        /**  判断用户是否登录        */
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var User  $currentUser */
        $currentUser = $guard->user();
        if(!$currentUser)
            throw new SecurityException(SecurityException::NoPermission);

        if ($id != 0 && ProjectMilestone::whereId($id)->exists())
            $ProjectMilestone = ProjectMilestone::find($id);
        else
            $ProjectMilestone = new ProjectMilestone();


        if(!$request->get('project_id'))
            throw new ProjectMilestoneException(ProjectMilestoneException::NoProjectId);

        if(!$request->get('mil_time'))
            throw new ProjectMilestoneException(ProjectMilestoneException::NoMilTime);

        if(!$request->get('mil_desc'))
            throw new ProjectMilestoneException(ProjectMilestoneException::NoMilDesc);

        $ProjectMilestone->project_id   = !is_null($request->get('project_id')) ? $request->get('project_id') : $ProjectMilestone->project_id;
        $ProjectMilestone->mil_time     = !is_null($request->get('mil_time')) ? $request->get('mil_time') : $ProjectMilestone->mil_time;
        $ProjectMilestone->mil_desc     = !is_null($request->get('mil_desc')) ? $request->get('mil_desc') : $ProjectMilestone->mil_desc;
        $ProjectMilestone->save();

        return $this->buildResponse(trans('api.project_milestone.success') , $ProjectMilestone );
    }



    /**
     * @param Guard $guard
     * @param ProjectNewsRequest $request           项目新闻的编辑
     * @param $id
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     * @throws ProjectException
     * @throws SecurityException
     */
    public function EditProjectNews(Guard $guard , ProjectNewsRequest $request , $id=0)
    {
        /** 判断用户是否登录*/

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var User  $currentUser */
        $currentUser = $guard->user();
        if(!$currentUser)
            throw new SecurityException(SecurityException::NoPermission);

        if($id != 0 && ProjectNews::whereId($id)->exists()){
            $ProjectNews = ProjectNews::find($id);
        }
        else{
            $ProjectNews = new ProjectNews();
            if(ProjectNews::whereNewsTitle($request->get('news_title'))->exists())
                throw new ProjectException(ProjectException::NewsTitleExists);
        }

        /** 接收数据 */
        $projectId      = $request->get('project_id');
        $newsTitle      = $request->get('news_title');
        $imgLogoId      = $request->get('img_logo_id');
        $newsIntro      = $request->get('news_intro');
        $newsComment    = $request->get('news_comment');
        $newsTime       = $request->get('news_time');
        $newsTag        = $request->get('news_tags');

        if(!is_null($projectId)) $ProjectNews->project_id = $projectId ;
        if(!is_null($newsTitle)) $ProjectNews->news_title = $newsTitle ;
        if(!is_null($imgLogoId)) $ProjectNews->img_logo_id = $imgLogoId ;
        if(!is_null($newsIntro)) $ProjectNews->news_intro = $newsIntro ;
        if(!is_null($newsComment)) $ProjectNews->news_comment = $newsComment ;
        if(!is_null($newsTime)) $ProjectNews->news_time = $newsTime ;
        if(!is_null($newsTag)) $ProjectNews->news_tags = $newsTag ;
        $ProjectNews->save();

        return $this->buildResponse(trans('api.invest_log.success') , $ProjectNews );
    }

    public function setProjectTeamByUserId(Guard $guard,ProjectTeamRequest $request){
        $name     = $request->get('name');
        $position = $request->get('position');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        $user = $guard->user();
        if(!$user)
            throw new SecurityException(SecurityException::NoPermission);

        $userTeam = new UserTeams();
        $userTeam->user_id     = $user->id;
        $userTeam->real_name   = $name;
        $userTeam->position    = $position;

        $userTeam->save();

        return $this->buildResponse(trans('api.set_user_team_by_userid.success'),$userTeam);
    }

    public function delProjectTeamById(Guard $guard){
        $id = Input::get('id');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var UserTeam $userTeam */
        $userTeam = UserTeams::whereId($id)->first();

        if($userTeam->user_id !== $guard->user()->id)
            throw new SecurityException(SecurityException::NoPermission);

        UserTeams::find($id)->delete();

        return $this->buildResponse(trans('api.del_project_team.success'));
    }

    /**
     * 通过团队logo_id 获取团队成员的logo
     * @param $photoId
     * @return mixed
     * @throws NotFoundException
     */
    public function getProjectTeamLogoByPhotoId($photoId)
    {
        $userTeam = UserTeams::wherePhotoId($photoId)->first();
        if(!$userTeam)
            throw new NotFoundException(NotFoundException::PhotoNotFound);

        $photo = $userTeam->toTeamPhoto;
        if(!$photo || $photo == null)
            return \Response::download('images/default_photo.jpg','default_photo.jpg',[],'inline');

        $photoFile = $photo->files;

        if(!$photoFile || $photoFile == null)
            return \Response::download('images/default_photo.jpg','default_photo.jpg',[],'inline');

        return \Response::download($photoFile->getLocalPath(),$photoFile->file_name,[],'inline');
    }


    /**`
     * 通过用户标识获取信息 判断用户是否拥有项目
     * @param $userId
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     */
    public function getProjectByUserId(Guard $guard)
    {
        $action = Input::get('action');
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        $userId = $guard->user()->id;

        $projects = Project::whereUserId($userId)->get();

        if($projects) {
            return $this->buildResponse(trans('api.check-is-has-project.success'), $projects);
        }else {
            return $this->buildResponse(trans('api.check-is-has-project.fail'),[],null,'error');
        }
    }

    /**
     * 提交项目到投资人
     * @param $proId
     * @param $investorId
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     */
    public function CommitProjectToInvestor(Guard $guard)
    {
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        $proId      = Input::get('projectId');
        $investorId = Input::get('investorId');

        /** 投资人不能提交项目 */
        $userId = $guard->user()->id;
        $role = User::find($userId)->role;
        if($role != 0)
            return $this->buildResponse(trans('You are not investor, can not submit the project'),[],null,"error");

        //判断项目是否已经提交
        if(InvestLog::whereProjectId($proId)->whereUserId($investorId)->exists())
            return $this->buildResponse(trans('api.project-has-been-submitted-to-investor.fail'),[],null,"error");

        if($proId && $investorId){
            $investLog = new InvestLog();
            $investLog->project_id  = $proId ;
            $investLog->user_id     = $investorId;
            $investLog->save();
            return $this->buildResponse(trans('api.user-commit-project-to-investor.success'));
        }else{
            return $this->buildResponse(trans('api.user-commit-project-to-investor.fail'),[],null,'error');
        }

    }

    /**
     * 项目提交到投资机构
     * @param $proId
     * @param $orgId
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     */
    public function CommitProjectToInvestOrg(Guard $guard)
    {
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        $proId = Input::get('projectId');
        $orgId = Input::get('orgId');

        //判断该项目是否已经提交、
        if(InvestLog::whereProjectId($proId)->whereOrgId($orgId)->exists())
            return $this->buildResponse(trans('api.project-has-been-submitted-to-org.fail'),[],null,'error');

        if($proId && $orgId){
            $investLog = new InvestLog();
            $investLog->project_id  = $proId ;
            $investLog->org_id      = $orgId ;
            $investLog->save();
            return $this->buildResponse(trans('api.user-commit-project-to-org.success'));
        }else{
            return $this->buildResponse(trans('api.user-commit-project-to-org.fail'),[],null,'error');
        }

    }

    /**
     * 投资人 跟投操作
     * @param Guard $guard
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     * @throws SecurityException
     */
    public function ThreadInvest(Guard $guard)
    {
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        $userId     = $guard->user()->id;
        $proId      = Input::get('projectId');
        $account    = Input::get('account');

        $role = User::find($userId)->role;
        if($role != 1)
           return $this->buildResponse(trans('You are the entrepreneur, can not lead or thread invest'),[],null, 'error');


        /** 判断投资金额是否小于最小金额 */
        $min_money = Project::find($proId)->min_money;
        if($account < $min_money)
            return $this->buildResponse(trans('The amount of investment is less than the minimum amount'),[],null,'error');

        if(InvestLog::whereUserId($userId)->whereProjectId($proId)->exists()){
            $investLog = InvestLog::whereUserId($userId)->whereProjectId($proId)->first();
            $investLog->invest_account = $account ;
            $investLog->save();
            $project =  new Project();
            $investLog->now_account    = $project->getNowInvestByProId($proId);       /** 获取当前融资金额 */
            return $this->buildResponse(trans('api.thread-invest-project-update.success') , $investLog);
        }else{
            if($userId && $proId && $account) {
                $investLog = new InvestLog();
                $investLog->project_id = $proId;
                $investLog->user_id = $userId;
                $investLog->invest_account = $account;
                $investLog->save();
                $investLog->now_account    = $account;
                return $this->buildResponse(trans('api.thread-invest-project-create.success'));
            }else{
                return $this->buildResponse(trans('api.thread-invest-project.fail'),[],null,'error');
            }
        }
    }

    /**
     * 投资人 领投操作
     * @param Guard $guard
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     * @throws SecurityException
     */
    public  function  LeadInvest(Guard $guard)
    {
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        $userId     = $guard->user()->id;
        $proId      = Input::get('projectId');
        $account    = Input::get('account');

        $role = User::find($userId)->role;
        if($role != 1)
            return $this->buildResponse(trans('You are the entrepreneur, can not lead or thread invest'),[],null,'error');

        /** 投资金额的限定 */
        $invet= Project::find($proId)->invet;
        if($account < 0.3*$invet)
            return $this->buildResponse(trans('Your lead-invest-account is too small.'),[],null,'error');

        /** 判断投资金额是否小于最小金额 */
        $min_money = Project::find($proId)->min_money;
        if($account < $min_money)
            return $this->buildResponse(trans('The amount of investment is less than the minimum amount'),[],null,'error');

        if(InvestLog::whereUserId($userId)->whereProjectId($proId)->exists()){
            $investLog = InvestLog::whereUserId($userId)->whereProjectId($proId)->first();
            $investLog->invest_account  = $account ;
            $investLog->is_lead         = 1;
            $investLog->save();
            $project =  new Project();
            $investLog->now_account     = $project->getNowInvestByProId($proId);
            return $this->buildResponse(trans('api.lead-invest-project-update.success') , $investLog);
        }else{
            if($userId && $proId && $account) {
                $investLog = new InvestLog();
                $investLog->project_id      = $proId;
                $investLog->user_id         = $userId;
                $investLog->invest_account  = $account;
                $investLog->is_lead         = 1;
                $investLog->save();

                $investLog->now_account     = $account;
                return $this->buildResponse(trans('api.lead-invest-project-create.success') , $investLog);
            }else{
                return $this->buildResponse(trans('api.lead-invest-project.fail'),[],null,'error');
            }
        }
    }

    /**
     * 通过 图片标识获取项目总览图片
     * @param $imgId
     * @return mixed
     */
    public function getProjectOverviewImgByImgId($imgId)
    {
        if($imgId == 0)
            return \Response::download('images/default_photo.jpg','default_photo.jpg',[],'inline');

        $image = Images::whereId($imgId)->first();


        if(!$image)
            return \Response::download('images/default_photo.jpg','default_photo.jpg',[],'inline');

        $photoFile = $image->files;
        return \Response::download($photoFile->getLocalPath(),$photoFile->file_name,[],'inline');
    }

    public function getProjectListBySearch()
    {
        $keyword = Input::get('keyword');
        $stage   = Input::get('stage');
        $phase   = Input::get('phase');
        $field   = Input::get('field');
        $dist    = Input::get('dist');

        $stage_key = !is_null($stage) ? $stage : '';
        $phase_key = !is_null($phase) ? $phase : '';
        $field_key = !is_null($field) ? $field : '';
        $dist_key  = !is_null($dist)  ? $dist  : '';

        if($field){
            /** @var Fields $f */
            $f = Fields::find($field);
            $projects = $f->project_x_fields()->whereProjectAuth(1);
        }else{
            /** @var Project $projects */
            $projects = Project::whereProjectAuth(1);
        }

        if($stage)
            $projects = $projects->like('is_finance',$stage);

        if($phase)
            $projects = $projects->like('phase_id',$phase);

        if($dist)
            $projects = $projects->like('province',$dist);

        $projects = $projects->get();

        foreach($projects as $project){
            $fields = $project->project_x_fields()->get();
            $projectTeams = $project->project_x_team()->get();
            $project['fields'] = $fields;
            $project['teams'] = $projectTeams;
            $project['phase'] = $project->toPhase;
        }

        return $this->buildResponse(trans('api.get_project_list_by_search.success'),$projects);

    }

    public function getFieldsByProjectId($projectId)
    {
        $fields = new Fields();

        /** @var Project $project */
        $project = Project::find($projectId);
        $fields = $project->project_x_fields()->get();
        return $this->buildResponse(trans('api.get_fields_by_project_id.success'),$fields);
    }

    /**
     * 静态刷新 页面
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     */
    public function getProjectMore()
    {
        $counts     = Project::whereProjectAuth(1)->count('id');    /** 总记录数 */
        $page       = Input::get('page');                           /** 页数 */
        $offset     = 0;
        if($page !=0 )
            $offset     = $page * self::PAGE ;

        if($counts >= $offset){
            $projects   = Project::whereProjectAuth(1)->skip($offset)->take(self::PAGE)->get();
            foreach($projects as $project)
            {
                $fields = $project->project_x_fields()->get();
                $projectTeams = $project->project_x_team()->get();
                $project['fields'] = $fields;
                $project['teams'] = $projectTeams;
                $project['phase'] = $project->toPhase;
            }

            return $this->buildResponse(trans('api.get_project_more.success'),$projects);
        }

    }


}